package com.hm.hall.commons.domain.resp;


import com.hm.hall.commons.constans.ErrorCode;

/**
 * 返回数据基类
 *
 * @author wei
 */
public class BaseResponse {
    /**
     * 返回码， 0标示成功，其他参考错误码定义类
     */
    public int errorCode;
    /**
     * 错误信息，默认为空字符串
     */
    public String errMsg = "";
    /**
     * 系统的时间戳**用于矫正手机本地时间不准的问题
     */
    public long sysTime;

    public BaseResponse() {
        sysTime = System.currentTimeMillis();
    }

    /**
     * @param errorCode
     * @param errMsg
     */
    public BaseResponse(int errorCode, String errMsg) {
        super();
        this.errorCode = errorCode;
        this.errMsg = errMsg;
        sysTime = System.currentTimeMillis();
    }


    /**
     * 传入枚举,提供类型检查,  防止出错
     *
     * @param errorCode 错误码枚举
     */
    public BaseResponse(ErrorCode errorCode) {
        this.errorCode = errorCode.code;
        this.errMsg = errorCode.msg;
        sysTime = System.currentTimeMillis();
    }

    /**
     * 设置返回500 , 的未知错误
     * e.getMessage() 提示错误
     */
    public BaseResponse(Exception e) {
        this.errorCode = ErrorCode.service_exception.code;
        this.errMsg = ErrorCode.service_exception.msg + e.getMessage();
        sysTime = System.currentTimeMillis();
    }

    /**
     * 传入枚举,提供类型检查,  防止出错
     *
     * @param errorCode 错误码枚举
     */
    public void setCodeAndMsg(ErrorCode errorCode) {
        this.errorCode = errorCode.code;
        this.errMsg = errorCode.msg;
        sysTime = System.currentTimeMillis();
    }


    /**
     * 设置返回0，空错误信息
     */
    public void setSuccessReturn() {
        this.errorCode = 0;
        this.errMsg = "";
        sysTime = System.currentTimeMillis();

    }

    /**
     * 设置返回500 , 的未知错误
     *
     * @param errMsg 设置错误消息
     */
    public void setUnknownError(String errMsg) {
        this.errorCode = ErrorCode.service_exception.code;
        this.errMsg = errMsg;
        sysTime = System.currentTimeMillis();
    }


    /**
     * 判断是否是OK 的状态
     */
    public boolean ok() {
        return this.errorCode == ErrorCode.get_ok.code;
    }


    /**
     * @return the errorCode
     */
    public int getErrorCode() {
        return errorCode;
    }

    /**
     * @param errorCode the errorCode to set
     */
    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * @return the errMsg
     */
    public String getErrMsg() {
        return errMsg;
    }

    /**
     * @param errMsg the errMsg to set
     */
    public void setErrMsg(String errMsg) {
        this.errMsg = errMsg;
    }

    /**
     * @return the sysTime
     */
    public long getSysTime() {
        return sysTime;
    }

    /**
     * @param sysTime the sysTime to set
     */
    public void setSysTime(long sysTime) {
        this.sysTime = sysTime;
    }


}

